﻿Imports System.ServiceModel.DomainServices.Client

Partial Public Class ThingsToDoAltaModificacio
    Inherits ChildWindow

    Private _ThingToDo As Web.ThingToDo
    Public ReadOnly Property ThingToDo As Web.ThingToDo
        Get
            Return _ThingToDo
        End Get
    End Property

    Private _Mode As EnumMode
    Public ReadOnly Property Mode As EnumMode
        Get
            Return _Mode
        End Get
    End Property

    Public Enum EnumMode
        ModeAlta
        ModeModificacio
    End Enum

#Region "Crear i Destruir"
    Public Sub New(pMode As EnumMode, pThingToDo As Web.ThingToDo)
        MyBase.New()

        InitializeComponent()
        _Mode = pMode
        _ThingToDo = pThingToDo
    End Sub


    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub ThingToDoAlta_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then
            Me.Activity.IsActive = True

            Me.cboUrgencies.ItemsSource = Contexte.Urgencies
            Me.cboRealizada.ItemsSource = (From o As OpcioCombo In OpcionsComboSiNo.Llista Where o.Id >= 0)

            AddHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats
            Contexte.LoadUsuaris()

            AddHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats
            Contexte.LoadHostals()


            AddHandler Contexte.CategoriesCarregades, AddressOf CategoriesCarregades
            Contexte.LoadCategories()

            AmagarCamps()

            Me.gridDetall.DataContext = _ThingToDo
        End If
    End Sub

    Private Sub AmagarCamps()
        Select Case Mode
            Case EnumMode.ModeAlta
                Me.Title = StringLibrary.AltaThingToDo

            Case EnumMode.ModeModificacio
                Me.Title = StringLibrary.EditarThingToDo

        End Select

        If Not Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
            cboUsuari.IsEnabled = False
            txtData.IsEnabled = False
        End If

        If Contexte.UsuariActual.IdTipusUsuari <> EnumTipusUsuari.Administrador AndAlso Contexte.UsuariActual.IdTipusUsuari <> EnumTipusUsuari.Manager Then
            cboHostals.IsEnabled = False
        End If

    End Sub
#End Region

#Region "Carregar Dades"
    Private _HostalsCarregats As Boolean = False
    Private _UsuarisCarregats As Boolean = False
    Private _CategoriesCarregades As Boolean = False

    Private Sub UsuarisCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.UsuarisCarregats, AddressOf UsuarisCarregats

        Select Case Mode
            Case EnumMode.ModeAlta
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    cboUsuari.ItemsSource = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
                Else
                    cboUsuari.ItemsSource = {Contexte.UsuariActual}

                End If

            Case EnumMode.ModeModificacio
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    cboUsuari.ItemsSource = (From U As LullabyFullDeCaixa.Web.Usuari In Contexte.Usuaris Where U.IdTipusUsuari <> EnumTipusUsuari.Neteja).ToList
                Else
                    cboUsuari.ItemsSource = {_ThingToDo.Usuari}
                End If

        End Select


        _UsuarisCarregats = True
        If _HostalsCarregats And _UsuarisCarregats And _CategoriesCarregades Then Me.Activity.IsActive = False
    End Sub

    Public Sub HostalsCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.HostalsCarregats, AddressOf HostalsCarregats

        Select Case Mode
            Case EnumMode.ModeAlta
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    Me.cboHostals.ItemsSource = Contexte.Hostals
                Else
                    Me.cboHostals.ItemsSource = Contexte.HostalsPerUsuariActual.AsEnumerable
                End If

            Case EnumMode.ModeModificacio
                If Contexte.UsuariActual.IdTipusUsuari = EnumTipusUsuari.Administrador Then
                    Me.cboHostals.ItemsSource = Contexte.Hostals
                Else
                    Me.cboHostals.ItemsSource = {_ThingToDo.Hostal}
                End If

        End Select

        If Me.cboHostals.Items.Count > 0 AndAlso Mode = EnumMode.ModeAlta Then _ThingToDo.Hostal = DirectCast(Me.cboHostals.Items(0), Web.Hostal)

        _HostalsCarregats = True
        If _HostalsCarregats And _UsuarisCarregats And _CategoriesCarregades Then Me.Activity.IsActive = False
    End Sub

    Public Sub CategoriesCarregades(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.CategoriesCarregades, AddressOf CategoriesCarregades
        _CategoriesCarregades = True

        Dim LListaCategories As List(Of Web.ThingsToDoCategory) = (From C As LullabyFullDeCaixa.Web.ThingsToDoCategory In Contexte.Categories Where Not C.EsBaixa).ToList

        Select Case Mode
            Case EnumMode.ModeAlta
                LListaCategories.Insert(0, New Web.ThingsToDoCategory() With {.Id = -1, .Nom = " "})
            Case EnumMode.ModeModificacio
                If _ThingToDo.Categoria.EsBaixa Then LListaCategories.Add(_ThingToDo.Categoria)
        End Select
        cboCategories.ItemsSource = LListaCategories

        If _HostalsCarregats And _UsuarisCarregats And _CategoriesCarregades Then Me.Activity.IsActive = False
    End Sub

#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() And Validacio.Errors.Count = 0 Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        If _ThingToDo.IdCategoria = -1 Then
            Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.DebeSeleccionarUnaCategoria))
            Resultat = False
        End If

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            App.ReportError(StringLibrary.ErrorGuardantDades)
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub
#End Region

End Class
